String.prototype.trim = function(){
	return this.replace(/^\s+|\s+$/g, "") ;
}


String.prototype.pad_left = function(len, pad) {
 	var str = this;
 	
	if (typeof(len) == "undefined") { var len = 0; }
	if (typeof(pad) == "undefined") { var pad = ' '; }
	
	if (len + 1 >= str.length) {
 		str = Array(len + 1 - str.length).join(pad) + str;
	}
 
	return str;
 
}


String.prototype.pad_right = function(len, pad) {
 	var str = this;
 	
	if (typeof(len) == "undefined") { var len = 0; }
	if (typeof(pad) == "undefined") { var pad = ' '; }
	
	if (len + 1 >= str.length) {
 		str = str + Array(len + 1 - str.length).join(pad);
	}
 
	return str;
 
}


String.prototype.is_date = function(){
	var regexp = /^((0|1|2)[1-9]|3(0|1))(0[1-9]|1[1-2])([0-9]{4})$/;
	return regexp.test(this);
}


String.prototype.is_number = function(){
	var n = this.trim();
	return !isNaN(parseFloat(n)) && isFinite(n);
}


Date.prototype.monthNames = [
    "January", "February", "March",
    "April", "May", "June",
    "July", "August", "September",
    "October", "November", "December"
];

Date.prototype.getRealMonth = function(){
	return this.getMonth() + 1;
};

Date.prototype.getMonthName = function() {
    return this.monthNames[this.getMonth()];
};

Date.prototype.getShortMonthName = function () {
    return this.getMonthName().substr(0, 3);
};


var mapUI = {
	jq_timer : null,
	
	jq_call : function(jq, p_callback, p_start){
		
		mapUI.jq_destroy_message();
		
		if(typeof p_start === 'function'){
			p_start();
		}
		
		$.ajax({
			 type: jq.method,
			 url: jq.url,
			 dataType: "json",
			 data: jq.postdata,
			 cache : false,
			 success: function(json) {
				if(typeof p_callback === 'function'){
					p_callback(json);
				}
			 },
			 
			 error: function(jqXHR, textStatus, errorThrown){
				// alert('HTTP error occured!' + errorThrown);
				console.log('HTTP error occured!' + errorThrown);
			 }
		});
	},
	
	
	jq_message : function(msg, wait_secs){
		if(this.jq_timer != null){
			clearTimeout(this.jq_timer);
		}
		
		
		mapUI.jq_destroy_message();
		
		var div = $('<div class="jq-message">' + msg + '</div>');
		div.click(function(event){
			$(this).remove();
		});
		
		
		$('body').append(div);
		
		if(wait_secs > 0){
			this.jq_timer = setTimeout(mapUI.jq_destroy_message, (wait_secs * 1000));
		} 
	},
	
	jq_destroy_message : function(){
		$('.jq-message').remove();
	},
	
	month_selector : function(class_name, data, cb){
		 function prev_month(selector, data, cb){
			var d = selector.find('.select-date').val();
			var m = selector.find('.select-month').val();
			var y = selector.find('.select-year').val();
			
			m = m - 1;	// javascript month is zero based 
			var m2 = m - 1;
			
			if(m2 < 0){
				m2 = 11;
				y = y - 1;
			}
			
			var dt = new Date(y, m2, d);
			selector.find('.select-year').val(dt.getFullYear());
			selector.find('.select-month').val(dt.getRealMonth());
			
			if(typeof cb === 'function'){
				cb(selector, dt, data);
			}
		}
		
		
		function next_month(selector, data, cb){
			var d = selector.find('.select-date').val();
			var m = selector.find('.select-month').val();
			var y = selector.find('.select-year').val();
			
			m = m - 1;	// javascript month is zero based 
			var m2 = m + 1;
			
			var dt = new Date(y, m2, d);
			selector.find('.select-year').val(dt.getFullYear());
			selector.find('.select-month').val(dt.getRealMonth());
			
			if(typeof cb === 'function'){
				cb(selector, dt, data);
			}
		}
		
		function input_change(selector, data, cb){
			var d = selector.find('.select-date').val();
			var m = selector.find('.select-month').val();
			var y = selector.find('.select-year').val();
			
			m = m - 1;	// javascript month is zero based 
			
			var dt = new Date(y, m, d);
			
			if(typeof cb === 'function'){
				cb(selector, dt, data);
			}
		}
		
		
		$('.' + class_name).each(function(index, elmt){
			var selector = $(this);
			var prev = selector.find('a.prev');
			var next = selector.find('a.next');
			
			prev.bind('click', function(){prev_month(selector, data, cb)});
			next.bind('click', function(){next_month(selector, data, cb)});
			
			var month_sel = selector.find('.select-month');
			month_sel.bind('change', function(){input_change(selector, data, cb); });
			
			var year_input = selector.find('.select-year');
			year_input.bind('blur', function(){input_change(selector, data, cb); });
		});
	
	}
}

